package com.example.demo;

import com.example.demo.utils.SpringContextUtils;
import com.example.demo.utils.StageUtils;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.IOException;

public class ExcelApplication extends Application {



    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(ExcelApplication.class.getResource("index.fxml"));
        fxmlLoader.setControllerFactory(SpringContextUtils::getBean);
        Scene scene = new Scene(fxmlLoader.load());
        stage.setOnCloseRequest(event -> {
            // 在这里关闭 Spring Boot 应用
            shutdownSpringBootApplication();
            Platform.exit(); // 关闭 JavaFX 应用
        });
        stage.setTitle("cqyTools");
        stage.setScene(scene);
        stage.show();
        StageUtils.stage = stage;

    }

    private void shutdownSpringBootApplication() {
        if (ToolsApplication.context != null) {
            ToolsApplication.context.close();
        }
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        shutdownSpringBootApplication();
    }

    public static void main(String[] args) {
        launch();
    }
}